home *** CD-ROM | disk | FTP | other *** search
- unit UCombo;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, FileCtrl;
-
- type
- TForm1 = class(TForm)
- ComboBox1: TComboBox;
- Label1: TLabel;
- procedure FormCreate(Sender: TObject);
- procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
- Rect: TRect; State: TOwnerDrawState);
- private
- { Private declarations }
- SysImageList: DWord;
- SIL_cx, SIL_cy: Integer;
- procedure AddDrive (DriveName: PChar);
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- uses ShellAPI, CommCtrl;
-
- {$R *.DFM}
-
- function Max (a, b: Integer): Integer;
- begin
- if a >= b then Result := a else Result := b;
- end;
-
- function GetItemHeight (Font: TFont): Integer;
- var
- dc: hDC;
- SaveFont: HFont;
- tm: TTextMetric;
- begin
- dc := GetDC (0);
- SaveFont := SelectObject (dc, Font.Handle);
- GetTextMetrics (dc, tm);
- Result := tm.tmHeight;
- SelectObject (dc, SaveFont);
- ReleaseDC (0, dc);
- end;
-
- function VolumeID (Path: PChar): String;
- var
- Junk: DWord;
- OldErrorMode: Integer;
- Buf: array [0..Max_Path] of Char;
- begin
- Result := '';
- OldErrorMode := SetErrorMode (sem_FailCriticalErrors);
- try
- if GetVolumeInformation (Path, Buf, sizeof (Buf), Nil, Junk, Junk, Nil, 0) then
- if Buf [0] <> #0 then Result := Result + ' ' + StrPas (Buf);
- finally
- SetErrorMode (OldErrorMode);
- end;
- end;
-
- function NetworkID (Path: PChar): String;
- var
- BufferSize: DWord;
- Buf: Array [0..Max_Path] of Char;
- begin
- BufferSize := sizeof (Buf);
- if WNetGetConnection (Path, Buf, BufferSize) = 0 then Result := StrPas (Buf)
- else Result := VolumeID (Path);
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- var
- p: PChar;
- sfi: TSHFileInfo;
- szDriveList: array [0..255] of Char;
- begin
- // Get a handle to the system image list
- SysImageList := SHGetFileInfo ('', 0, sfi, sizeof (sfi),
- shgfi_SysIconIndex or shgfi_SmallIcon);
-
- // Calculate itemHeight of the combo box
- ImageList_GetIconSize (SysImageList, SIL_cx, SIL_cy);
- ComboBox1.ItemHeight := Max (SIL_cy, GetItemHeight (ComboBox1.Font)) +
- (2 * GetSystemMetrics (sm_cyBorder));
- ComboBox1.Style := csOwnerDrawFixed;
-
- // Fill combo box with drive letters
- p := szDriveList;
- GetLogicalDriveStrings (sizeof (szDriveList), szDriveList);
- while p^ <> #0 do begin
- AddDrive (p);
- Inc (p, 4);
- end;
-
- ComboBox1.ItemIndex := 0;
- end;
-
- procedure TForm1.AddDrive (DriveName: PChar);
- var
- S: String;
- begin
- S := Copy (UpperCase (StrPas (DriveName)), 1, 2);
-
- case GetDriveType (DriveName) of
- drive_Fixed,
- drive_CDROM,
- drive_RAMDisk: S := S + VolumeID (DriveName);
- drive_Remote: S := S + NetworkID (DriveName);
- end;
-
- ComboBox1.Items.Add (S);
- end;
-
- procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
- var
- X, Y: Integer;
- shfi: TSHFileInfo;
- szBuff: array [0..255] of Char;
- begin
- with Control as TComboBox, Canvas do begin
- FillRect (Rect);
- // Figure out the image list index for this drive
- SHGetFileInfo (StrPCopy (szBuff, Copy (Items [Index], 1, 2) + '\'),
- 0, shfi, sizeof (shfi), shgfi_Icon or shgfi_SmallIcon);
- X := Rect.Left + 2 * GetSystemMetrics (sm_cxBorder);
- Y := Rect.Top + ((Rect.Bottom - Rect.Top - SIL_cy) div 2);
- ImageList_DrawEx (SysImageList, shfi.iIcon, Handle, X, Y, 0, 0,
- clr_None, clr_None, ild_Transparent);
- Inc (Rect.Left, (SIL_cx * 3) div 2);
- DrawText (Handle, PChar (Items [Index]), -1, Rect, dt_Left or dt_VCenter);
- end;
- end;
-
- end.
-
-